package cn.stylefeng.guns.modular.file.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.file.entity.FileEntity;
import cn.stylefeng.guns.modular.file.mapper.FileMapper;
import cn.stylefeng.guns.modular.file.model.params.FileParam;
import cn.stylefeng.guns.modular.file.model.result.FileResult;
import cn.stylefeng.guns.modular.file.service.FileEntityService;
import cn.stylefeng.guns.modular.file.service.FileService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 文件管理 服务实现类
 * </p>
 *
 * @author rebiekong
 * @since 2019-10-20
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

    @Autowired
    private FileEntityService fileEntityService;
    @Override
    public void add(FileParam param){
        File entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void add(FileParam param, FileEntity fileEntity) {
        File entity = getEntity(param);
        entity.setLatestFileVersion(fileEntity.getVersion());
        this.save(entity);
        fileEntity.setFileId(entity.getId());
        fileEntityService.save(fileEntity);
    }

    @Override
    public void delete(FileParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FileParam param){
        File oldEntity = getOldEntity(param);
        File newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateFile(Long fileId, FileEntity fileEntity) {
        File newEntity = this.baseMapper.lockFile(fileId);
        Integer currentVersion = newEntity.getLatestFileVersion() + 1;
        fileEntity.setVersion(currentVersion);
        fileEntity.setFileId(fileId);
        newEntity.setLatestFileVersion(currentVersion);
        fileEntityService.save(fileEntity);
        this.updateById(newEntity);
    }

    @Override
    public FileResult findBySpec(FileParam param){
        return null;
    }

    @Override
    public List<FileResult> findListBySpec(FileParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FileParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FileParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private File getOldEntity(FileParam param) {
        return this.getById(getKey(param));
    }

    private File getEntity(FileParam param) {
        File entity = new File();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
